
/**
 * 
 */
package com.htht.smartschool.service.roles;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.htht.smartschool.common.service.BaseService;
import com.htht.smartschool.model.roles.AdminRole;
import com.htht.smartschool.model.roles.IntoschoolRoles;
import com.htht.smartschool.model.roles.RoleStudent;
import com.htht.smartschool.model.student.TStudent;
import com.htht.smartschool.model.user.TUser;
import com.sun.org.apache.xpath.internal.operations.And;

/**
 * @author wuqiwei
 *
 */
public class RolesService extends BaseService {

	/**查询学生进出校信息
	 * @author:wuqiwei
	 * @param @param intoschoolRoles
	 * @param @return
	 * @date:2016年1月20日下午3:05:37
	*/
	public List<IntoschoolRoles> getIntoSchoolRole(
			IntoschoolRoles intoschoolRoles) {
		String sql = "select * from t_intoschool_role where id >= '1'";
		if(intoschoolRoles.getImei() != null && intoschoolRoles.getImei().length() >0){
			sql +=" and imei = :imei";
		}
		if(intoschoolRoles.getRolecode() != null && intoschoolRoles.getRolecode().length() >0){
			sql +=" and rolecode = :rolecode";
		}
		if(intoschoolRoles.getClasscode() != null && intoschoolRoles.getClasscode().length()>0){
			sql +=" and classcode = :classcode";
		}
		if(intoschoolRoles.getGrade() != null && intoschoolRoles.getGrade().length()>0){
			sql +=" and grade = :grade";
		}
		if(intoschoolRoles.getTheclass() != null && intoschoolRoles.getTheclass().length()>0){
			sql +=" and class = :theclass";
		}
		if(intoschoolRoles.getTimeroles() != null && intoschoolRoles.getTimeroles().length()>0){
			sql +=" and timeroles = :timeroles";
		}
		List<Object[]> list = getDao().queryBySQL(sql,intoschoolRoles); 
		List<IntoschoolRoles> result = new ArrayList<IntoschoolRoles>();
		for (Object[] obj : list) {
			IntoschoolRoles into = new IntoschoolRoles();
			into.setImei(String.valueOf(obj[1]));
			into.setRolecode(String.valueOf(obj[2]));
			into.setClasscode(String.valueOf(obj[3]));
			into.setGrade(String.valueOf(obj[4]));
			into.setTheclass(String.valueOf(obj[5]));
			into.setTimeroles(String.valueOf(obj[6]));
			result.add(into);
		}
		return result;
	}

	/**删除信息
	 * @author:wuqiwei
	 * @param @param intoschoolRoles
	 * @param @return
	 * @date:2016年1月20日下午3:33:40
	*/
	public boolean deleteIntoschoolRole(IntoschoolRoles intoschoolRoles) {
		boolean flag = false;
		if(intoschoolRoles != null && intoschoolRoles.getId().equals("")){
			String sql ="delete from t_intoschool_role where id = :id";
			int state=getDao().executeSQL(sql,intoschoolRoles);
			if (state >= 0) {
				flag = true;
			}
		}
		return flag;
	}

	/**修改进出校info
	 * @author:wuqiwei
	 * @param @param intoschoolRoles
	 * @param @return
	 * @date:2016年1月20日下午3:44:53
	*/
	public boolean updateIntoschoolRole(IntoschoolRoles intoschoolRoles) {
		String sql ="update  t_intoschool_role set  timeroles = :timeroles, remark = :remark where id >=1";
		if(intoschoolRoles.getImei() != null && intoschoolRoles.getImei().length() >0){
			sql +=" and imei = :ime";
		}
		if(intoschoolRoles.getRolecode() != null && intoschoolRoles.getRolecode().length() >0){
			sql +=" and rolecode = :rolecode";
		}
		if(intoschoolRoles.getClasscode() != null && intoschoolRoles.getClasscode().length()>0){
			sql +=" and classcode = :classcode";
		}
		if(intoschoolRoles.getGrade() != null && intoschoolRoles.getGrade().length()>0){
			sql +=" and grade = :grade";
		}
		if(intoschoolRoles.getTheclass() != null && intoschoolRoles.getTheclass().length()>0){
			sql +=" and class = ：theclass";
		}
		int a = getDao().executeSQL(sql,intoschoolRoles);
		if(a == -1){
			return false;
		}else{
			return true;
		}
	}

	/**插入数据
	 * @author:wuqiwei
	 * @param @return
	 * @date:2016年1月20日下午4:20:41
	*/
	public boolean insertIntoschoolRole(IntoschoolRoles intoschoolRoles) {
		String sql = "insert into t_intoschool_role (id,imei,rolecode,classcode,grade,class,timeroles,remark) values(null,:imei,:rolecode,:classcode,:grade,:theclass,:timeroles,:remark)";
		int i = getDao().executeSQL(sql,intoschoolRoles);
		return i == -1 ? false :true;
	}

	/**查询规则
	 * @author:wuqiwei
	 * @param @param intoschoolRoles
	 * @param @return
	 * @date:2016年1月20日下午5:10:02
	*/
	public List<AdminRole> queryAdminRole(AdminRole adminRole) {
		String sql = "select * from t_admin_role where id >= 1";
		if(adminRole.getUsercode() != null && adminRole.getUsercode().length() >0){
			sql += " and usercode = :usercode";
		}
		if(adminRole.getUsertype() != null && adminRole.getUsertype().length() >0){
			sql += " and usertype = :usertype";
		}
		if(adminRole.getRoles()  != null && adminRole.getRoles().length() >0){
			sql += " and roles = :roles";
		}
		if(adminRole.getRemark() != null && adminRole.getRemark().length() >0){
			sql += " and remark = :remark";
		}
		List<Object[]> list = getDao().queryBySQL(sql,adminRole); 
		List<AdminRole>  result = new ArrayList<AdminRole>();
		for (Object[] obj : list) {
			AdminRole into = new AdminRole();
			into.setUsercode(String.valueOf(obj[1]));
			into.setUsertype(String.valueOf(obj[2]));
			into.setRoles(String.valueOf(obj[3]));
			into.setRemark(String.valueOf(obj[4]));
			result.add(into);
		}
		return result;
	}

	/**删除规则
	 * @author:wuqiwei
	 * @param @param adminRole
	 * @param @return
	 * @date:2016年1月20日下午5:30:28
	*/
	public boolean deleteAdminRole(AdminRole adminRole) {
		boolean flag = false;
		if(adminRole != null && adminRole.getId().equals("")){
			String sql ="delete from t_admin_role where id = :id";
			int state=getDao().executeSQL(sql,adminRole);
			if (state >= 0) {
				flag = true;
			}
		}
		return flag;
	}

	/**修改规则
	 * @author:wuqiwei
	 * @param @param adminRole
	 * @param @return
	 * @date:2016年1月20日下午5:32:51
	*/
	public boolean updateAdminRole(AdminRole adminRole) {
		String sql ="update  t_admin_role set  roles = :roles, remark = :remark where id >=1";
		if(adminRole.getUsercode() != null && adminRole.getUsercode().length() >0){
			sql += " and usercode = :usercode";
		}
		if(adminRole.getUsertype() != null && adminRole.getUsertype().length() >0){
			sql += " and usertype = :usertype";
		}
		int a = getDao().executeSQL(sql,adminRole);
		if(a == -1){
			return false;
		}else{
			return true;
		}
	}

	/**插入规则数据
	 * @author:wuqiwei
	 * @param @param adminRole
	 * @param @return
	 * @date:2016年1月20日下午5:39:39
	*/
	public boolean insertAdminRole(AdminRole adminRole) {
		String sql1 ="select * from t_admin_role where usercode = :usercode and usertype = :usertype"; 
		List<Object[]> list = getDao().queryBySQL(sql1,adminRole); 
		if(list.size() > 0){
			String del = "delete from t_admin_role where usercode = :usercode and usertype = :usertype";
			getDao().executeSQL(del,adminRole);
		}
		String sql = "insert into t_admin_role (id,usercode,usertype,roles,remark) values (null,:usercode,:usertype,':roles,:remark)";
		int i = getDao().executeSQL(sql,adminRole);
		return i == -1 ? false :true;
	}

	/**批量添加考勤规则
	 * @author:wuqiwei
	 * @param @param roleStudent
	 * @param @return
	 * @date:2016年1月21日上午10:28:58
	*/
	public boolean barchAddRole(RoleStudent roleStudent) {
		boolean flag = false;
//		if(roleStudent.getStudentcode().equalsIgnoreCase("")){//如果studentCode等于空的时候就清空这个班级下的角色与学生关系信息
			deleteRoleStudnet(roleStudent);//删除班级下的角色与学生的关系信息
//		}
		String studentcodes = roleStudent.getStudentcode();
		String[] stCodeArr = studentcodes.split(",");
//		String delsql = "DELETE FROM t_role_student WHERE studentcode IN(:studentcode)";
//		getDao().executeSQL(delsql,roleStudent);
		String sql = "insert into t_role_student (id,rolecode,studentcode,classcode) values ";
		for (int i = 0; i < stCodeArr.length; i++) {
			sql +="(null,:rolecode,'"+stCodeArr[i]+"',:classcode),";
		}
		sql = sql.substring(0,sql.length()-1);
		int a = getDao().executeSQL(sql,roleStudent);
		if(a == -1){
			flag = false;
		}else{
			flag = true;
		}
		return flag;
	}

	/**查询考勤规则学生信息
	 * @author:wuqiwei
	 * @param @param roleStudent
	 * @param @return
	 * @date:2016年1月21日上午11:11:18
	*/
	@SuppressWarnings("unchecked")
	public List<RoleStudent> queryRoleStudent(RoleStudent roleStudent) {
		String sql = "select * from t_role_student where id >= '1'";
		if(roleStudent.getRolecode() != null && roleStudent.getRolecode().length() >0){
			sql += " and rolecode = :rolecode";
		}
		if(roleStudent.getStudentcode() != null && roleStudent.getStudentcode().length() >0){
			sql += " and studentcode = :studentcode";
		}
		List<Object[]> list = getDao().queryBySQL(sql,roleStudent); 
		List<RoleStudent> result= new ArrayList<RoleStudent>();
		for (Object[] obj : list) {
			RoleStudent role = new RoleStudent();
			role.setRolecode(String.valueOf(obj[1]));
			role.setStudentcode(String.valueOf(obj[2]));
			result.add(role);
		}
		return result;
	}
	@SuppressWarnings("unchecked")
	public List<RoleStudent> queryRoleStudentByClass(RoleStudent roleStudent) {
		String sql = "SELECT distinct trs.rolecode,trs.studentcode,ts.studytype,ts.studentname,tar.name FROM t_role_student trs,"
				+ "(select name studentname,studytype,studentcode,classcode from t_student where classcode = :classcode) ts,"
				+ "t_class tc,t_attendance_regular tar WHERE trs.rolecode = tar.code AND trs.studentcode = ts.studentcode AND ts.classcode = tc.classcode";
		if(roleStudent.getRolecode() != null && roleStudent.getRolecode().length() >0){
			sql += " and trs.rolecode = :rolecode";
		}
		if(roleStudent.getStudentcode() != null && roleStudent.getStudentcode().length() >0){
			sql += " and trs.studentcode = :studentcode";
		}
		if(roleStudent.getClasscode() != null && roleStudent.getClasscode().length() > 0){
			sql += " and tc.classcode = :classcode";
		}
		if(roleStudent.getSchoolcode() != null && roleStudent.getSchoolcode().length() > 0){
			sql += " and tc.schoolcode = :schoolcode";
		}
		if(roleStudent.getName() != null && roleStudent.getName().length() > 0){
			sql += " and tc.name like :name";
			roleStudent.setName("%"+roleStudent.getName()+"%");
		}
		List<Object[]> list = getDao().queryBySQL(sql,roleStudent); 
		List<RoleStudent> result= new ArrayList<RoleStudent>();
		Map<String,RoleStudent> map = new HashMap<String,RoleStudent>();
		for (Object[] obj : list) {
			RoleStudent role = new RoleStudent();
			role.setRolecode(String.valueOf(obj[0]));
			role.setStudentcode(String.valueOf(obj[1]));
			role.setStudytype(String.valueOf(obj[2]));
			role.setName(String.valueOf(obj[3]));
			role.setRolename(String.valueOf(obj[4]));
			result.add(role);
			map.put(role.getStudentcode(), role);
		}
		String hql = "From TStudent where classcode = :classcode";
		if(roleStudent.getSchoolcode() != null && roleStudent.getSchoolcode().length() > 0){
			hql += " and schoolcode = :schoolcode";
		}
		List<TStudent> liststu = getDao().queryByHQL(hql,roleStudent);
		for (TStudent stu : liststu) {
			RoleStudent role = new RoleStudent();
			role.setRolecode("");
			role.setStudentcode(stu.getStudentcode());
			role.setStudytype(stu.getStudytype());
			role.setName(stu.getName());
			role.setRolename("");
			RoleStudent old = map.get(role.getStudentcode());
			if(old == null){
				result.add(role);
			}
		}
		return result;
	}

	/**修改考勤规则信息
	 * @author:wuqiwei
	 * @param @param roleStudent
	 * @param @return
	 * @date:2016年1月22日上午9:12:24
	*/
	public boolean updateRoleStudent(RoleStudent roleStudent) {
		String sql = "update t_role_student set rolecode = '"+roleStudent.getRolecode()+"' where id >= '1' ";
		if(roleStudent.getStudentcode() != null && roleStudent.getStudentcode().length() >0){
			sql += " and studentcode = '"+roleStudent.getStudentcode()+"'";
		}
		return false;
	}

	/**删除考勤规则
	 * @author:wuqiwei
	 * @param @param roleStudent
	 * @param @return
	 * @date:2016年1月22日上午9:18:39
	*/
	public boolean deleteRoleStudnet(RoleStudent roleStudent) {
		boolean flag = false;
		String sql = " delete from t_role_student where classcode = :classcode";
		int a = getDao().executeSQL(sql,roleStudent);
		if (a >= 0) {
			flag = true;
		}
		return flag;
	}

	/**插入规则数据
	 * @author:wuqiwei
	 * @param @param roleStudent
	 * @param @return
	 * @date:2016年1月22日上午9:23:44
	*/
	public boolean insertRoleStudent(RoleStudent roleStudent) {
		String sql = "insert into t_role_student (id,rolecode,studentcode) values(null,:rolecode,:studentcode)";
		int a = getDao().executeSQL(sql,roleStudent);
		return a == -1 ? false :true;
	}

}
